package com.nikkerball.web.base;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Marius
 * Date: 16-Jan-2010
 * Time: 11:29:04
 */
public class StumpsLink<T extends Page> extends Panel {


    private List<StumpsLinkClickListener> clickListeners = new ArrayList<StumpsLinkClickListener>();

    public StumpsLink(String id, Class<T> page, String text) {
        super(id);
        InternalLink<T> link = new InternalLink<T>("link", page);
        Label textLabel = new Label("text", text);
        textLabel.setRenderBodyOnly(true);
        link.add(textLabel);
        add(link);
    }

    public void addClickListener(StumpsLinkClickListener listener) {
        this.clickListeners.add(listener);
    }

    class InternalLink<T extends Page> extends Link {

        private Class<T> page;

        public InternalLink(String id, Class<T> page) {
            super(id);
            this.page = page;
        }

        @Override
        public void onClick() {
            for (StumpsLinkClickListener listener : clickListeners) {
                listener.linkClicked();
            }
            setResponsePage(page);
        }
    }
}